package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nshmura.recyclertablayout.RecyclerTabLayout;
import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.SysApplication;
import com.tepth.maintenancesystem.api.MaintainHttp;
import com.tepth.maintenancesystem.api.OutLogin;
import com.tepth.maintenancesystem.bean.Contents;
import com.tepth.maintenancesystem.bean.MaintainDriverAndRemark;
import com.tepth.maintenancesystem.bean.MaintainListWork;
import com.tepth.maintenancesystem.bean.VehicleMaintain;
import com.tepth.maintenancesystem.presenter.InitViewPager;
import com.tepth.maintenancesystem.presenter.MaintainDetailsAdapter;
import com.tepth.maintenancesystem.presenter.MaintainDetailsCompletedRightAdapter;
import com.tepth.maintenancesystem.presenter.MaintainDetailsLeftAdapter;
import com.tepth.maintenancesystem.untils.DimensionConvert;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MaintainCompletedDetailsActivity extends AppCompatActivity implements InitViewPager, OutLogin {

    private MaintainDetailsLeftAdapter leftAdapter;
    private MaintainDetailsCompletedRightAdapter rightAdapter;
    private ListView leftListView;
    private ListView rightListView;

    private Context context;
    private ArrayList<MaintainListWork> listCompleteds = new ArrayList<>();
    private VehicleMaintain obj;
    private LoadingDialog loadingDialog;
    private RecyclerTabLayout mRecyclerTabLayout;
    private RelativeLayout rl_work_back;
    private TextView tv_work_title, tv_maintain_remark;
    private MaintainDriverAndRemark remark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain_completed_details);
        SysApplication.getInstance().addActivity(this);
        context = this;
        Bundle bundle = this.getIntent().getBundleExtra("MaintainBundle");
        obj = (VehicleMaintain) bundle.getSerializable("Maintain");
        MaintainDetailsAdapter adapter = new MaintainDetailsAdapter(this);
        ViewPager viewPager = (ViewPager) findViewById(R.id.maintain_completed_pager);
        viewPager.setAdapter(adapter);
        mRecyclerTabLayout = (RecyclerTabLayout) findViewById(R.id.maintain_completed_tablayout);
        mRecyclerTabLayout.setTabMinWidth(DimensionConvert.getWidth2(this));//设置tab宽度
        mRecyclerTabLayout.setUpWithViewPager(viewPager);

        tv_work_title = (TextView) findViewById(R.id.tv_work_completed_title);
        tv_work_title.setText(obj.getVehicleNumber() + "  保养作业");
        rl_work_back = (RelativeLayout) findViewById(R.id.rl_work_completed_back);
        rl_work_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initViewPager(View view, int position) {
        switch (position) {
            case 0:
                initView(view);
                getDriverAndRemark();
                break;
            case 1:
                break;
            case 2:
                break;
        }
    }

    private void initView(View view) {
        leftListView = (ListView) view.findViewById(R.id.left_listview);
        rightListView = (ListView) view.findViewById(R.id.right_listview);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        lp.setMargins(0, 80, 0, 0);
        view.findViewById(R.id.linear_main_list).setLayoutParams(lp);
        view.findViewById(R.id.rl_work_barcode).setVisibility(View.GONE);
        view.findViewById(R.id.linear_guojian_barcode).setVisibility(View.GONE);
        view.findViewById(R.id.linear_junjian_barcode).setVisibility(View.VISIBLE);
        ((TextView) view.findViewById(R.id.tv_junjian_text)).setText("查看竣检");
        view.findViewById(R.id.linear_junjian_barcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("Maintain", obj);
                intent.putExtra("MaintainBundle", bundle);
                intent.setClass(context, MaintainCompletedDoneActivity.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.linear_bottom).setVisibility(View.GONE);
//        view.findViewById(R.id.tv_guojian_bottom_name).setVisibility(View.GONE);
        tv_maintain_remark = (TextView) view.findViewById(R.id.tv_maintain_remark);
    }

    private void getDriverAndRemark() {
        if (remark != null) {
            getListWorkContentsNormal();
            return;
        }
        loadingDialog = new LoadingDialog(context);
        loadingDialog.setInterceptBack(false)
                .setLoadingText("正在加载...");
        loadingDialog.show();
        new Thread() {
            @Override
            public void run() {
                remark = MaintainHttp.getDriverAndRemark(context, obj.getId() + "");
                myHandler.sendEmptyMessage(3);
                super.run();
            }
        }.start();
    }

    /**
     * 请求保养项目列表
     */
    private void getListWorkContentsNormal() {
        if (listCompleteds.size() > 0) {
            initLeftListView();
            return;
        }
        new Thread() {
            @Override
            public void run() {
                listCompleteds = MaintainHttp.getListWork(context, obj.getId(), MaintainCompletedDetailsActivity.this);
                myHandler.sendEmptyMessage(0);
                super.run();
            }
        }.start();
    }

    private void initLeftListView() {
        leftAdapter = new MaintainDetailsLeftAdapter(this, listCompleteds);
        leftAdapter.setSelectPosition(0);
        leftListView.setAdapter(leftAdapter);
        initRightListView(listCompleteds.get(0).getListContents());
        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                updataRightListView(position);
            }
        });
    }

    private void updataRightListView(int position) {
        leftAdapter.setSelectPosition(position);
        leftAdapter.notifyDataSetChanged();
        //更新第二ListView
        rightAdapter.setDatas(listCompleteds.get(position).getListContents());
        rightAdapter.notifyDataSetChanged();
    }

    private void initRightListView(List<Contents> listContents) {
        rightAdapter = new MaintainDetailsCompletedRightAdapter(this, listContents);
        rightListView.setAdapter(rightAdapter);
        rightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Contents object = (Contents) rightAdapter.getItem(i);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("contents", object);
                bundle.putString("workName", leftAdapter.getCurrentPositionItem().getContentTypeName());
                bundle.putString("vehicleName", obj.getVehicleNumber());
                intent.putExtra("bundle", bundle);
                intent.setClass(context, MaintainWorkCompletedDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    loadingDialog.close();
                    if (listCompleteds != null) {
                        initLeftListView();
                    } else {
                        ToastUtils.showToast(context, "网络错误");
                    }
                    break;
                case 1:
                    ToastUtils.showToast(context, "登录超时");
                    Intent intent2 = new Intent();
                    intent2.setClass(context, LoginActivity.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent2.putExtra("manual", "manual");
                    startActivity(intent2);
                    break;
                case 3:
                    tv_maintain_remark.setText("报修信息:" + remark.getRemark());
                    getListWorkContentsNormal();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void outLogin() {
        myHandler.sendEmptyMessage(1);
    }
}
